$(function(){

	//操作提示展开收起
	$(document).on("click", "#explanationZoom",function(){
		var explanation = $(this).parents(".explanation");
		var width = $(".content").width();
		if($(this).hasClass("shopUp")){
			$(this).removeClass("shopUp");
			$(this).attr("title","收起提示");
			explanation.find(".ex_tit").css("margin-bottom",10);
			explanation.animate({
				width:width-28
			},300,function(){
				$(".explanation").find("ul").show();
			});
		}else{
			$(this).addClass("shopUp");
			$(this).attr("title","提示相关设置操作时应注意的要点");
			explanation.find(".ex_tit").css("margin-bottom",0);
			explanation.animate({
				width:"100"
			},300);
			explanation.find("ul").hide();
		}
	});


	$("div.treeMenu>ul>li>div.title").click(function(){
		var that = $(this).parent();
		that.siblings().removeClass("current").end().addClass("current");
		var submenu = that.find(".sub-menu");
		if(submenu.hasClass("on")){
			submenu.slideUp().removeClass("on");
			if(submenu.length !=0 ) $(this).find("span").attr("class", "icon icon-angle-right");
		}else {
			$(".sub-menu.on").slideUp().removeClass("on");
			submenu.slideDown().addClass("on");
			if(submenu.length !=0 ) $(this).find("span").attr("class", "icon icon-angle-down");
		}
	});

	$("div.sub-menu>div").click(function(){
		$(this).siblings().removeClass("active").end().addClass("active");
		var target = $(this).attr("target");
		loadHtml(target, true);
	});

	$(document).on("click", "div#url, a#url, button#url", function(){
		loadHtml($(this).attr("data-url"), true);
	});

	//div仿select下拉选框 start
	$(document).on("click",".imitate_select .cite",function(){
		$(".imitate_select ul").not($(this).parents(".imitate_select").find("ul")).slideUp(300);
		$(this).parents(".imitate_select").find("ul").slideToggle(300);
	});

	//上传管理员头像
	$("#_pic").change(function(){
		var actionUrl = ctx + "/user/uploadAvatar";
		$("#fileForm").ajaxSubmit({
			type: "POST",
			dataType: "json",
			url: actionUrl,
			data: { "action": "TemporaryImage" },
			success: function (res) {
				if (res.code == 0) {
					alert(res.data);
					//$(".avatar img").attr("src", data.content);
				}else
					alert(res.message);
			},
			async: true
		});
	});

	$(document).on("click",".imitate_select li  a",function(){
		var val = $(this).data('value');
		var text = $(this).html();
		$(this).parents(".imitate_select").find(".cite").html(text);
		$(this).parents(".imitate_select").find("input[type=hidden]").val(val);
		$(this).parents(".imitate_select").find("ul").slideUp(300);
	});
	$(document).bind("click", function(e) {
		// 隐藏下拉
		if (!$($(e.target)[0]).parents(".imitate_select").length) {
			$(".imitate_select").find("ul").slideUp(300);
		}
	});
	//div仿select下拉选框 end

	//属性选中效果 by wu start
	$(document).on("click",".checkbox_items label",function(){
		if($(this).siblings("input[type=checkbox]:first").prop("checked") == true){
			$(this).siblings("input[type=checkbox]:first").prop("checked",false);
		}else{
			$(this).siblings("input[type=checkbox]:first").prop("checked",true);
		}
	});
	//属性选中效果 by wu end

	$.fn.serializeJson = function () {
		var serializeObj = {};
		$(this.serializeArray()).each(function () {
			serializeObj[this.name] = this.value;
		});
		return serializeObj;
	};
	//jquery 插件对象
	/**
	 * 重置表单
	 * @param {Object} o
	 * @memberOf {TypeName}
	 * @return {TypeName}
	 */
	$.fn.resetObjectForm = function(jsonValue){
		var obj = this;
		$.each(jsonValue, function (name, ival) {
			var $oinput = obj.find("input[name=" + name + "]");
			if ($oinput.attr("type") == "checkbox") {
				if (ival !== null) {
					var checkboxObj = $("[name=" + name + "]");
					var checkArray = ival.split(";");
					for (var i = 0; i < checkboxObj.length; i++) {
						for (var j = 0; j < checkArray.length; j++) {
							if (checkboxObj[i].value == checkArray[j]) {
								checkboxObj[i].click();
							}
						}
					}
				}
			}
			else if ($oinput.attr("type") == "radio") {
				$oinput.each(function () {
					var radioObj = $("[name=" + name + "]");
					for (var i = 0; i < radioObj.length; i++) {
						if (radioObj[i].value == ival) {
							radioObj[i].click();
						}
					}
				});
			}
			else if ($oinput.attr("type") == "textarea") {
				obj.find("[name=" + name + "]").html(ival);
			}
			else {
				obj.find("[name=" + name + "]").val(ival);
			}
		})

	};

	//重写Alert美化UI
	//var alertFun = window.alert;
	//window.alert=function(msg){
	//	alertFun(msg);
	//}
	////重写confirm美化UI
	//var confirmFun = window.confirm;
	//window.confirm=function(msg){
	//	confirmFun(msg);
	//}

	//顶部管理员信息展开
	//顶部管理员信息展开
	var adminSetup = function(){
		var hoverTimer, outTimer;
		$('#admin-manager-btn,.manager-menu,.admincp-map').mouseenter(function(){
			clearTimeout(outTimer);
			hoverTimer = setTimeout(function(){
				$('.manager-menu').show();
				$('#admin-manager-btn').removeClass().addClass("arrow-close");
			},200);
		});

		$('#admin-manager-btn,.manager-menu,.admincp-map').mouseleave(function(){
			clearTimeout(hoverTimer);
			outTimer = setTimeout(function(){
				$('.manager-menu').hide();
				$('#admin-manager-btn').removeClass().addClass("arrow");
			},100);
		});
	}
	adminSetup();

	//消息通知
	var message = function(){
		var hoverTimer, outTimer;
		$('.msg,#msg_Container').mouseenter(function(){
			clearTimeout(outTimer);
			hoverTimer = setTimeout(function(){
				$('#msg_Container').show();
			},200);
		});

		$('.msg,#msg_Container').mouseleave(function(){
			clearTimeout(hoverTimer);
			outTimer = setTimeout(function(){
				$('#msg_Container').hide();
			},100);
		});
	}
	message();

	$(window).resize(function() {
		$("div.treeMenu").css("height", ($(document.body).height() - 50) +"px");
		$("div.mainFrame").css("width", ($(document.body).width() - 200) +"px");
	});
	$("div.treeMenu").css("height", ($(document.body).height() - 50) +"px");
	$("div.mainFrame").css("width", ($(document.body).width() - 220) +"px");

});

var loadHtml = function(target, isMask){
	if(isMask)$("div.mainFrame").html('<div class="loading-box"><div class="loading"></div><img class="loading-logo" src="'+ctx+'/static/images/loading-logo.png" /></div>');
	$.load(target, function(res, status, xhr){
			setTimeout(function(){
				$("div.mainFrame").html(res);
			}, 1000);
		}
	);
}